#include <bits/stdc++.h>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};

class Solution
{
public:
    vector<double> averageOfLevels(TreeNode *root)
    {
        vector<double> ret;
        queue<TreeNode *> level;
        level.push(root);
        while (!level.empty())
        {
            int count = level.size();
            double sum = 0;
            for (int i = 0; i < count; ++i)
            {
                TreeNode *tmp = level.front();
                level.pop();
                sum += tmp->val;
                if (tmp->left)
                {
                    level.push(tmp->left);
                }
                if (tmp->right)
                {
                    level.push(tmp->right);
                }
            }
            sum /= count;
            ret.push_back(sum);
        }
        return ret;
    }
};